package com.liujit.pay.wx.domain.params.order;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

/**
 * @description: 商品列表
 * @author: liujun
 * @create: 2021-02-20 09:57
 */
public class OrderGoodsDetail {

    /**
     * 商户侧商品编码
     */
    private String merchantGoodsId;

    /**
     * 微信侧商品编码
     */
    private String wechatPayGoodsId;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 商品数量
     */
    private Integer quantity;

    /**
     * 商品单价
     */
    private Integer unitPrice;

    OrderGoodsDetail(String merchantGoodsId, String wechatPayGoodsId, String goodsName, Integer quantity, Integer unitPrice) {
        this.merchantGoodsId = merchantGoodsId;
        this.wechatPayGoodsId = wechatPayGoodsId;
        this.goodsName = goodsName;
        this.quantity = quantity;
        this.unitPrice = unitPrice;
    }

    /**
     * 初始化参数
     *
     * @param merchantGoodsId 商户侧商品编码
     * @param quantity        商品数量
     * @param unitPrice       商品单价
     * @return
     */
    public static OrderGoodsDetail.Builder builder(String merchantGoodsId, Integer quantity, Integer unitPrice) {
        return new OrderGoodsDetail.Builder(merchantGoodsId, quantity, unitPrice);
    }

    public static class Builder {

        private String merchantGoodsId;
        private String wechatPayGoodsId;
        private String goodsName;
        private Integer quantity;
        private Integer unitPrice;

        Builder(String merchantGoodsId, Integer quantity, Integer unitPrice) {
            this.merchantGoodsId = merchantGoodsId;
            this.quantity = quantity;
            this.unitPrice = unitPrice;
        }

        /**
         * 设置微信侧商品编码
         *
         * @param wechatPayGoodsId 微信侧商品编码
         * @return
         */
        public OrderGoodsDetail.Builder wechatPayGoodsId(String wechatPayGoodsId) {
            this.wechatPayGoodsId = wechatPayGoodsId;
            return this;
        }

        /**
         * 设置商品名称
         *
         * @param goodsName 商品名称
         * @return
         */
        public OrderGoodsDetail.Builder goodsName(String goodsName) {
            this.goodsName = goodsName;
            return this;
        }

        public OrderGoodsDetail build() {
            return new OrderGoodsDetail(this.merchantGoodsId, this.wechatPayGoodsId, this.goodsName, this.quantity, this.unitPrice);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public JSONObject toJSONObject() {
        JSONObject json = new JSONObject();
        // 商户侧商品编码
        json.set("merchant_goods_id", merchantGoodsId);
        // 微信侧商品编码
        if (StrUtil.isNotBlank(wechatPayGoodsId)) {
            json.set("wechatpay_goods_id", wechatPayGoodsId);
        }
        // 商品名称
        if (StrUtil.isNotBlank(goodsName)) {
            json.set("goods_name", goodsName);
        }
        // 商品数量
        json.set("quantity", quantity);
        // 商品单价
        json.set("unit_price", unitPrice);
        return json;
    }
}
